<template>
    <div style="min-height: 400px">
        <BasicForm @register="registerForm"></BasicForm>
        <div style="width: 100%;text-align: center" v-if="!formDisabled">
            <a-button @click="submitForm" pre-icon="ant-design:check" type="primary">提 交</a-button>
        </div>
    </div>
</template>

<script lang="ts">
    import {BasicForm, useForm} from '/@/components/Form/index';
    import {computed, defineComponent} from 'vue';
    import {defHttp} from '/@/utils/http/axios';
    import { propTypes } from '/@/utils/propTypes';
    import { useJvxeMethod } from '/@/hooks/system/useJvxeMethods';
    import { VALIDATE_FAILED } from '/@/utils/common/vxeUtils';
    import {getBpmFormSchema,webWorkFileColumns} from '../WebWorkDynamic.data';
    import {saveOrUpdate,webWorkFileList} from '../WebWorkDynamic.api';
    
    export default defineComponent({
        name: "WebWorkDynamicForm",
        components:{
            BasicForm
        },
        props:{
            formData: propTypes.object.def({}),
            formBpm: propTypes.bool.def(true),
        },
        setup(props){
            const [registerForm, { setFieldsValue, setProps, getFieldsValue }] = useForm({
                labelWidth: 150,
                schemas: getBpmFormSchema(props.formData),
                showActionButtonGroup: false,
                baseColProps: {span: 24}
            });

            const formDisabled = computed(()=>{
                if(props.formData.disabled === false){
                    return false;
                }
                return true;
            });
const refKeys = ref(['webWorkFile', ]);
      const activeKey = ref('webWorkFile');
      const webWorkFile = ref();
      const tableRefs = {webWorkFile, };
      const webWorkFileTable = reactive({
        loading: false,
        dataSource: [],
        columns:webWorkFileColumns,
        show: false
      })

      const [handleChangeTabs,handleSubmit,requestSubTableData,formRef] = useJvxeMethod(requestAddOrEdit,classifyIntoFormData,tableRefs,activeKey,refKeys,validateSubForm);

      function classifyIntoFormData(allValues) {
        let main = Object.assign({}, allValues.formValue)
        return {
          ...main, // 展开
          webWorkFileList: allValues.tablesValue[0].tableData,
        }
      }
//表单提交事件
      async function requestAddOrEdit(values) {
        await saveOrUpdate(values, true);
      }

        const queryByIdUrl = '/website/webWorkDynamic/queryById';
            async function initFormData(){
              let params = {id: props.formData.dataId};
              const data = await defHttp.get({url: queryByIdUrl, params});
              //设置表单的值
              await setFieldsValue({...data});
              requestSubTableData(webWorkFileList, {id: data.id}, webWorkFileTable, ()=>{
                webWorkFileTable.show = true;
              });
              //默认是禁用
              await setProps({disabled: formDisabled.value})
            }
        
            initFormData();
        
            return {
              registerForm,
              formDisabled,
              formRef,
              handleSubmit,
              activeKey,
              handleChangeTabs,
              webWorkFile,
              webWorkFileTable,
            }
          }
        });
</script>
